<?php

namespace App\Library\Message\Notifications;

use App\Library\Message\Channels\SmsChannel;
use App\Components\User\Models\MobileVerificationCode;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class VerificationCode extends Notification
{
    private $mobileVerificationCode;

    public function __construct(MobileVerificationCode $mobileVerificationCode)
    {
        $this->mobileVerificationCode = $mobileVerificationCode;
        $this->queue = 'message';
    }

    public function via($notifiable)
    {
        return [SmsChannel::class];
    }

    public function toSms($notifiable)
    {
        return [
            'sign' => 'default',
            'template' => 'mobile_verify',
            'param' => [
                'code' => $this->mobileVerificationCode->code,
            ],
        ];
    }

    public function onSendFinished($driver, $result)
    {
        if ($driver == 'sms') {
            $this->mobileVerificationCode->update([
                'result_request_id' => $result->RequestId,
                'result_code' => $result->Code,
                'result_message' => $result->Message,
                'result_biz_id' => $result->BizId ?? null,
            ]);
        }
    }
}
